/*<![CDATA[*/
/**
 * 新增设备
 */
function addDeviceAction() {
	if (userType != '3') {
		_alert("只有小区管理员可以新增设备");
		return;
	}
	var search = $("#search").val();
	var isFlag = "add";
	var url = encodeURI("/IMS/device/editDevice.ht?communityName="
			+ communityName + "&isFlag=" + isFlag + "&cid=" + cid + "&search="
			+ search);
	locationHref(url);
}

/**
 * 维护设备地址
 */
function editDeviceAddressAction() {
	var search = $("#search").val();
	var url = encodeURI("/IMS/deviceAddress/showList.ht?communityName="
			+ communityName + "&cid=" + cid + "&search=" + search);
	locationHref(url);
}
/**
 * 编辑删除设备
 */
function editDeviceAction(deviceId, status, deviceCode, deviceName,
		communityName, serviceCode, chargeName, orgNo, orgName, addressId) {
	var isFlag = "edit";
	var search = $("#search").val();
	var url = encodeURI("/IMS/device/editDevice.ht?communityName="
			+ communityName + "&deviceId=" + deviceId + "&chargeName="
			+ chargeName + "&cid=" + cid + "&orgNo=" + orgNo + "&orgName="
			+ orgName + "&isFlag=" + isFlag + "&serviceCode=" + serviceCode
			+ "&deviceCode=" + deviceCode + "&deviceName=" + deviceName
			+ "&status=" + status + "&search=" + search + "&addressId="
			+ addressId);
	locationHref(url);
}

/**
 * 批量更新设备状态
 */
function batchEditDevice(status) {
	var url = "/IMS/device/batchUpdateDevice.ht?cid=" + cid + "&status="
			+ status;
	updateDevice("frm", url, "是否更新所选状态");
}

/*******************************************************************************
 * 更新设备
 ******************************************************************************/
function updateDevice(formId, _url, msg) {
	if (!formId || typeof _url === "String")
		return;
	if (!_url || typeof _url === "String")
		return;
	if (!$("input[name='ids']")) {
		_alert("没有可选项");
		return false;
	}
	if ($("#" + formId + " input[name='ids']:checked").length <= 0) {
		_alert("请选择您要更新的记录");
		return false;
	}

	if (!msg)
		msg = '是否更新所选记录';
	_confirm("<span style='color:#FE6600'>" + msg + "</span>", function() {
		var form = document.getElementById(formId);
		form.action = _url;
		form.submit();

	});
}

/**
 * 批量删除当前设备
 */
function batchDeleteDeviceAction() {
	
	if (!$("input[name='ids']")) {
		_alert("没有可选项");
		return false;
	}
	
	var chk_value =[];
	$('input[name="ids"]:checked').each(function(){ 
		chk_value.push($(this).val()); 
	});
	
	if(chk_value.length<=0){
		_alert("请选择您要删除的记录");
		return;
	} 
	
	_confirm('<span style="color:#FE6600">是否删除当前记录</span>', function() {
		var _url = "/IMS/device/batchDeleteDevice.ht";
		var _data = {
			ids : chk_value.join(",")
		};
		var search = $("#search").val();
		$.ajax({
			url : _url,
			async : false,
			type : "post",
			data : _data,
			dataType : "json",
			success : function(value) {
				var code = value.status;
				if (code == message_success_code) {
					var listUrl = "/IMS/device/showList.ht?cid=" + cid
							+ "&search=" + search;
					locationHref(listUrl);
				} else {
					_alert(value.msg);
					return;
				}
			},
			error : function(value, a, b) {
				_alert(value.status);
				return;
			}
		});
	});
}

/**
 * 删除当前设备
 */
function deleteDeviceAction(deviceId) {

	_confirm('<span style="color:#FE6600">是否删除当前记录</span>', function() {
		var _url = "/IMS/device/deleteDevice.ht";
		var _data = {
			deviceId : deviceId
		};
		var search = $("#search").val();
		$.ajax({
			url : _url,
			async : false,
			type : "post",
			data : _data,
			dataType : "json",
			success : function(value) {
				var code = value.status;
				if (code == message_success_code) {
					var listUrl = "/IMS/device/showList.ht?cid=" + cid
							+ "&search=" + search;
					locationHref(listUrl);
				} else {
					_alert(value.msg);
					return;
				}
			},
			error : function(value, a, b) {
				_alert(value.status);
				return;
			}
		});
	});
}

$(document).ready(function() {
	$("#cid").select2({
		language : "zh-CN",
		placeholder : "请选择小区",
		allowClear : true,
		ajax : {
			url : "/IMS/select/getCommunityList.ht",
			dataType : 'json',
			delay : 250,
			data : function(param) {
				return {
					search : param.term,// 输入的内容，会发给服务器进行查找
					// 以下两行可以在数据量大的时候用到（当下拉框往下拉的时候会加载下一页），数据量不大的时候，可以直接注释
					page : param.page || 1, // 当前页
					showAllItem : true
				};
			},
			processResults : function(data, param) {
				param.page = data.pageNum;
				return {
					results : data.list,
					pagination : {
						more : data.hasNextPage
					}
				};
			}
		},
		escapeMarkup : function(markup) {
			return markup;
		},
		templateResult : formatRepo, // 显示查询结果
		templateSelection : function(obj) {
			$("#cidForHidden").val(obj.id);
			$("#communityName").val(obj.name);
			return obj.name || obj.text;
		} // 显示选中的对象
	});
});

function searchSubmit(){
	var communityId = $("#select2-cid-container").text();
	var search = $("#search").val();
	if(communityId=='请选择小区'&&!search){
		_alert("请选择小区");
		return;
	}else{
		if(userType!=3){
			$('#frm').submit();
		}else{
			var search = $("#search").val();
			if(Is_Null(search)){
				_alert("请输入设备名称或设备号");
				return;
			}
			$('#frm').submit();
		}
	}
}
/* ]]> */